package com.errands.cn.controller;

import com.errands.cn.entity.Evaluate;
import com.errands.cn.entity.OrderReceiving;
import com.errands.cn.entity.Orders;
import com.errands.cn.entity.User;
import com.errands.cn.services.EvaluateService;
import com.errands.cn.services.OrderRecivingService;
import com.errands.cn.services.OrderService;
import com.errands.cn.utils.Json;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private OrderRecivingService recivingService;
    @Autowired
    private OrderService orderService;

    @PostMapping("/evaluate/add")
    @Transactional
    public boolean addEvaluate(@RequestParam("oId")String oId, @RequestParam("content") String content,
                    @RequestParam("userType")Integer userType, HttpServletRequest request){
        OrderReceiving receiving = recivingService.getOrderReceingByoId(oId);
        System.out.println(oId);
        System.out.println(receiving);
        Evaluate evaluate = new Evaluate();
        if (userType == 1){
            evaluate.setAcceptUid(receiving.getUid());
            evaluate.setEvaluateUid(receiving.getSendUid());
            Orders orders = new Orders();
            orders.setoId(oId);
            orders.setoState(3);
            orderService.updateOrder(orders);

        }else {
            evaluate.setAcceptUid(receiving.getSendUid());
            evaluate.setEvaluateUid(receiving.getUid());
            OrderReceiving receiving1 = new OrderReceiving();
            receiving1.setId(receiving.getId());
            receiving1.setState(3);
            recivingService.update(receiving1);
        }
        evaluate.setContent(content);
        evaluate.setDate(new Date());
        evaluate.setoId(oId);
        return evaluateService.save(evaluate);
    }

    @GetMapping("/evaluate/delete")
    public boolean deleteEvaluate(@RequestParam("eId") Integer eId){
       return evaluateService.deleteEvaluate(eId);
    }

    @GetMapping("/evaluate/get_e")
    public List<Evaluate> getByEvaluate(HttpServletRequest request){
      //  User user = (User) request.getSession().getAttribute("user");
        return evaluateService.getByEvaluate(12);
    }
    @GetMapping("/evaluate/get_a")
    public List<Evaluate> getByAccept(HttpServletRequest request){
        //  User user = (User) request.getSession().getAttribute("user");
        return evaluateService.getByAccept(12);
    }
}
